﻿using System;
using System.Collections.Generic;
using AMM.Business;
using AMM.Business.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.UserControls {
    public partial class OutsourceOrderSearchControl : BaseSearchControl {

        public override BaseSearchModel GetSearchModel() {
            int vendorValue = Convert.ToInt32(this.ddlVendor.SelectedValueEx);
            int currencyValue = Convert.ToInt32(this.cmbCurrency.SelectedValue);
            int deliverStatusValue = Convert.ToInt32(this.cmbDeliverStatus.SelectedValue);
            int orderStatusValue = Convert.ToInt32(this.cmbOrderStatus.SelectedValue);
            int printStatusValue = Convert.ToInt32(this.cmbPrintStatus.SelectedValue);

            OutsourceOrderSearchModel searchResult = new OutsourceOrderSearchModel();
            searchResult.Code = this.tbCode.Text;
            searchResult.VendorId = vendorValue;
            searchResult.ProductCode = this.tbProductCode.Text;
            searchResult.User = this.tbUserName.Text;
            searchResult.CreateDateStart = this.GetDateTimePickerValue(this.dtpCreateStart);
            searchResult.CreateDateEnd = this.GetDateTimePickerValue(this.dtpCreateEnd);
            searchResult.DeliverDateStart = this.GetDateTimePickerValue(this.dtpDeliverStart);
            searchResult.DeliverDateEnd = this.GetDateTimePickerValue(this.dtpDeliverEnd);
            searchResult.PayCurrencyId = currencyValue;
            searchResult.DeliverStatus = deliverStatusValue;
            searchResult.OrderStatus = orderStatusValue;
            searchResult.PrintStatus = printStatusValue;

            if (vendorValue == 0) { searchResult.VendorId = null; }
            if (currencyValue == 0) { searchResult.PayCurrencyId = null; }
            if (deliverStatusValue == 0) { searchResult.DeliverStatus = null; }
            if (orderStatusValue == -1) { searchResult.OrderStatus = null; }
            if (printStatusValue == 0) { searchResult.PrintStatus = null; }

            return searchResult;
        }

        public override void ResetSearchModel() {
            this.ddlVendor.SelectedValueEx = null;
            this.tbCode.Clear();
            this.tbProductCode.Clear();
            this.tbUserName.Clear();
            this.dtpCreateStart.Checked = false;
            this.dtpCreateEnd.Checked = false;
            this.dtpDeliverStart.Checked = false;
            this.dtpDeliverEnd.Checked = false;
            this.cmbCurrency.SelectedIndex = 0;
            this.cmbDeliverStatus.SelectedIndex = 0;
            this.cmbOrderStatus.SelectedIndex = 0;
            this.cmbPrintStatus.SelectedIndex = 0;
        }

        private void BindVendor() {
            VendorSearchModel searchModel = new VendorSearchModel() { IsEnabled = true };
            VendorProcessor vendorProcessor = new VendorProcessor(int.MaxValue);
            List<VendorViewModel> vendorList = vendorProcessor.FetchVendorViewModelList(searchModel);

            this.ddlVendor.DisplayMemberEx = "Code";
            this.ddlVendor.ValueMemberEx = "Key";
            this.ddlVendor.HeightEx = 300;
            this.ddlVendor.WidthEx = 400;
            this.ddlVendor.PageSizeEx = 20;
            this.ddlVendor.DataBind(new VendorViewModel(), x => new { x.Code, x.Name, x.Address }, vendorList);
        }

        private void BindDeliverStatus() {
            List<object> resultList = new List<object>();
            resultList.Add(new { Text = "", Value = "0" });
            resultList.Add(new { Text = "已交齐", Value = "1" });
            resultList.Add(new { Text = "已交货", Value = "2" });
            resultList.Add(new { Text = "未交货", Value = "3" });

            this.cmbDeliverStatus.DataSource = resultList;
            this.cmbDeliverStatus.DisplayMember = "Text";
            this.cmbDeliverStatus.ValueMember = "Value";
        }

        private void BindPrintStatus() {
            List<object> resultList = new List<object>();
            resultList.Add(new { Text = "", Value = "0" });
            resultList.Add(new { Text = "已打印", Value = "1" });
            resultList.Add(new { Text = "未打印", Value = "2" });

            this.cmbPrintStatus.DataSource = resultList;
            this.cmbPrintStatus.DisplayMember = "Text";
            this.cmbPrintStatus.ValueMember = "Value";
        }

        public OutsourceOrderSearchControl() {
            this.InitializeComponent();
        }

        private void OutsourceOrderSearchControl_Load(object sender, EventArgs e) {
            this.BindBasics(this.cmbCurrency, BasicsType.Currency);
            this.BindAuditStatus(this.cmbOrderStatus);
            this.BindDeliverStatus();
            this.BindPrintStatus();
            this.BindVendor();
        }
    }
}
